//双向链表的模拟实现
public class MyLinkedList {

    //创建节点类
    static class Node{
        //成员变量
        int val;
        Node prev;
        Node next;

        public Node(int val){
            this.val = val;
        }
    }

    //定义头节点和尾节点
    Node head;
    Node last;


        //头插法
        public void addFirst(int data){
            //创建一个节点
            Node node = new Node(data);
            //首次增加
            if(head == null){
                head = node;
                last = node;
            }//已有元素
            else{
                node.next = head;
                head.prev = node;
                head = node;
            }
        }
        //尾插法
        public void addLast(int data){
            //创建节点
            Node node = new Node(data);

            //首次加入节点
            if(last == null){
                //设置头节点和尾部节点
                head = node;
                last = node;
            }//非首次加入
            else{
                last.next = node;
                node.prev = last;
                last = node;
            }
        }
        //任意位置插⼊,第⼀个数据节点为0号下标
        public void addIndex(int index,int data){
            //1.坐标需要判断是否合法，不合法需要抛异常
            if(index < 0 || index > size()){
                throw new IndexException("坐标非法：" + index);
            }

            //2.插入节点
            //是否是头插
            if(index == 0){
                addFirst(data);
                return;
            }
            //是否是尾部插入
            if(index == size()){
                addLast(data);
                return;
            }

            //中间插入：找到插入元素的前一个节点或后一个节点
            Node cur = searchNode(index);
            //创建一个节点
            Node node = new Node(data);
            //1.设置cur节点的上一个节点的next
            cur.prev.next = node;
            //2.node节点的设置
            node.next = cur;
            node.prev = cur.prev;
            //3.改变cur的prev
            cur.prev = node;
        }

        private Node searchNode(int index){
            //创建一个临时引用
            Node tem = head;
            while(index > 0){
                tem = tem.next;
                index--;
            }
            return tem;
        }
        //查找是否包含关键字key是否在单链表当中
        public boolean contains(int key){
           //遍历节点查找
            Node tem = head;
            while(tem != null){
                if(tem.val == key){
                    return true;
                }
                tem = tem.next;
            }
            return false;
        }
        //删除第⼀次出现关键字为key的节点
        public void remove(int key){
            //没有节点，不需要删除
            if(head == null) return;
            //遍历节点，找到节点后删除
            Node tem = head;
            while(tem != null){
                //找到了删除
                if(tem.val == key){
                    //1.只有一个节点
                    if(size() == 1){
                        head = null;
                        last = null;
                    }else{
                        //删除头节点
                        if(tem == head){
                            tem.next.prev = head.prev;
                            head = head.next;
                        }//删除尾节点
                        else if(tem == last){
                            tem.prev.next = last.next;
                            last = last.prev;
                        }//删除中间节点
                        else{
                            tem.prev.next = tem.next;
                            tem.next.prev = tem.prev;
                        }
                    }
                    return;
                }
                tem = tem.next;
            }
        }
        //删除所有值为key的节点
        public void removeAllKey(int key){
            //没有节点，不需要删除
            if(head == null) return;
            //只有一个节点
            if(size() == 1){
                head = null;
                last = null;
            }

            //遍历每一个节点，逐个删除
            //因为头节点和尾节点的删除需要移动位置，可以先删除
            //头节点和尾节点中间值为key的节点，最后在判断头节点和尾节点
            //定义两个节点cur 和 curN，curN用来判断是否是需要删除的节点
            //cur是curN的上一个节点，cur从头节点开始
            Node cur = head;
            Node curN = head.next;
            //删除中间节点
            while(curN.next != null){
                //删除节点
                if(curN.val == key){
                    cur.next = curN.next;
                    curN.next.prev = cur;
                    //删除后curN往后移动
                    curN = curN.next;
                }else{
                    cur = curN;
                    curN = curN.next;
                }
            }
            //判断是否需要删除头节点或者尾节点
             if(last.val == key){
                 last.prev.next = last.next;
                 last = last.prev;
             }
            if(head.val == key){
                //此时删除后可能就只有一个节点
             if(head.next == null){
                 last = null;
                 head =null;
             }else{
                 head.next.prev = head.prev;
                 head = head.next;
             }
            }

        }
        //得到单链表的⻓度
        public int size(){
            if(head == null) return 0;
            Node tem = head;
            int cnt = 0;
            while(tem != null){
                cnt++;
                tem = tem.next;
            }
            return cnt;
        }

        public void display(){
            Node tem = head;
            System.out.print("[");
            while(tem != null) {
                if(tem.next != null)
                System.out.print(tem.val + ",");
                else
                    System.out.print(tem.val);
                tem = tem.next;
            }
            System.out.println("]");
        }

        public void clear(){
             //1.将每一个节点的next和prev赋值为null
            Node cur = head;
            Node curN = head.next;
            while(curN != null){
                curN.prev = null;
                cur.next = null;
                cur = curN;
                curN = curN.next;
            }
            //2.将head和last赋值为null
            head = null;
            last = null;
        }


}
